package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.DishDao;
import cn.usian.dao.DishFlavorDao;
import cn.usian.pojo.Dish;
import cn.usian.pojo.DishDto;
import cn.usian.pojo.DishFlavor;
import cn.usian.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Resource
    private DishDao dishDao;

    @Resource
    private DishFlavorDao dishFlavorDao;
    //分页
    @Override
    public R findPage(int page, int pageSize, String name) {
        Page<DishDto>pageInfo=new Page<>(page,pageSize);
        List<DishDto>list=dishDao.findPage(pageInfo,name);
        //将list设置到pageinfo中
        pageInfo.setRecords(list);
        return R.success(pageInfo);
    }

    //添加
    @Override
    public void save(DishDto dishDto) {
       /* //1.补全菜品的数据

        //2.保存菜品
        dishDao.insert(dishDto);
        //3.得到菜品的口味信息
        List<DishFlavor>flavorList=dishDto.getFlavors();
        //4.给一个口味信息补全
        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId());//口味对应的菜品的id
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setCreateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setIsDeleted(0);
        }
        //5.批量插入彩品口味
        for (DishFlavor df : flavorList) {
            dishFlavorDao.insert(df);
        }*/
        //1.保存菜品的基本信息到菜品表dish
        dishDao.insert(dishDto);

        Long dishId = dishDto.getId();//菜品ID


        List<DishFlavor> flavorList = dishDto.getFlavors();

        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
            dishFlavor.setCreateUser(dishDto.getCreateUser());
            dishFlavor.setIsDeleted(0);
        }

        for (DishFlavor df : flavorList) {
            dishFlavorDao.insert(df);
        }
    }
    //查询id
    @Override
    public R findById(Long id) {
        //先根据id查询dish
        Dish dish = dishDao.selectById(id);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        //再根据dish_id查询对应的口味
        List<DishFlavor>list=dishFlavorDao.selectList(lambdaQueryWrapper);
        //返回dishid查询对应的口味
        DishDto dishDto = new DishDto();
        //将dish对象中的数据复制到dishDto对象中
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return R.success(dishDto);
    }
    //修改
    @Override
    public void updateDish(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateTime(LocalDateTime.now());
        dishDao.updateById(dishDto);
        LambdaUpdateWrapper<DishFlavor> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        dishFlavorDao.delete(lambdaUpdateWrapper);
        List<DishFlavor> flavorList = dishDto.getFlavors();

        for (DishFlavor dishFlavor : flavorList) {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setCreateTime(dishDto.getCreateTime());
            dishFlavor.setUpdateTime(dishDto.getUpdateTime());
            dishFlavor.setUpdateUser(dishDto.getUpdateUser());
        }

        for (DishFlavor df : flavorList) {
            dishFlavorDao.insert(df);
        }
    }

    @Override
    public void del(Long[] ids) {
        for (Long id : ids) {
            dishDao.deleteById(id);
        }
    }

    @Override
    public R status(Integer status, Long[] ids) {
        Dish dish = new Dish();
        for (Long id : ids) {
            dish.setId(id);
            dish.setStatus(status);
            dish.setUpdateTime(LocalDateTime.now());
            dish.setCreateTime(LocalDateTime.now());
            dishDao.updateById(dish);
        }
        return R.success("修改成功");
    }

}
